package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/yaotian/gowechat"
	"github.com/yaotian/gowechat/mp/message"
	"github.com/yaotian/gowechat/wxcontext"
)

type MainController struct {
	beego.Controller
}

func (this *MainController) Post()  {
	var config = wxcontext.Config{
		AppID: beego.AppConfig.String("AppId"),
		AppSecret: beego.AppConfig.String("AppSecret"),
		Token: beego.AppConfig.String("Token"),
	}

	//fmt.Println(config)

	var wc = gowechat.NewWechat(config)
	mp, err := wc.MpMgr()
	if err!=nil {
		fmt.Println(err)
		return
	}

	//传入request和responseWriter
	msgHandler := mp.GetMsgHandler(this.Ctx.Request, this.Ctx.ResponseWriter)

	msgHandler.SetHandleMessageFunc(func(msg message.MixMessage) *message.Reply {
		fmt.Println(msg.MsgType)
		// 回复消息
		text := message.NewText(msg.Content)
		return  &message.Reply{message.MsgTypeText, text}
	})

	err = msgHandler.Handle()
	if err!= nil {
		beego.Error(err)
	}
}
